<?php namespace Common\Model;
use Hdphp\Model\Model;
class Cate extends Model{
	protected $table='category';
	//自动验证
	protected $validate=array(
	array('cname','required','分类名称不能为空',3,3)
	);

	//分类表的添加
	public function store(){
		if(!$this->create()) return false;
		$this->data['cname']=explode('|', $_POST['cname']);
//		p($this->data);
		foreach ($this->data['cname'] as $k => $v) {
				$this->data['cname']=$v;
				$this->add($this->data);
		}
	
		return true;
		
	}
	//子类的添加
	public function storeSon($pid){
		if(!$this->create()) return false;
		$this->data['cname']=explode('|', $_POST['cname']);
		foreach ($this->data['cname'] as $v) {
			$this->data['cname']=$v;
			$this->add($this->data);
			
		}

		return true;
	}
//	//分类表的删除
	public function del($cid){
		$cateData=$this->get();
		foreach ($cateData as $v) {
			if($v['pid']==$cid){
				$this->error='请删除本分类下的子类';
				return false;
			}
		}
		$this->where(array('cid'=>$cid))->delete();
		return true;
	}
	//获取除了自己之外的所有父类,也可以找包括自己在内的子类getSon
	public function getNoMy($cid,$pid){
		
		if($pid==0){
			$ab='顶级分类';
			return $ab;
		}
		$data=$this->get();
		$cids=$this->getSon($data,$cid);
		$cids[]=$cid;
		$cateData=$this->where('cid not in('.implode(',', $cids).')')->get();
		$cateData=Data::tree($cateData,'cname');
		
		return $cateData;
	}
	public function getSon($data,$cid){
		$temp=array();
		foreach ($data as $v) {
			if($v['pid']==$cid){
				$temp[]=$v['cid'];
				$temp=array_merge($temp,$this->getSon($data,$v['cid']));
			}
		}
		return $temp;
	}
	public function edit($cid){
		if(!$this->create()) return false;
		$this->where(array('cid'=>$cid))->save();
		return true;
	}
}



 ?>